<template>
  <div class="app-container notice-container">
    <!-- 搜索和操作区域合并 -->
    <el-card class="search-operation-card" shadow="hover">
      <div class="card-header">
        <h3 class="card-title">公告搜索与操作</h3>
      </div>
      
      <!-- 搜索区域 -->
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px" class="search-form">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="是否展示" prop="isShow">
              <el-select v-model="queryParams.isShow" placeholder="请选择是否展示" clearable class="form-select">
                <el-option
                  v-for="dict in is_or_not"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="显示位置" prop="position">
              <el-select v-model="queryParams.position" placeholder="请选择显示位置" clearable class="form-select">
                <el-option
                  v-for="dict in position"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="search-actions">
              <el-button type="primary" icon="Search" @click="handleQuery" class="search-btn">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery" class="reset-btn">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      
      <!-- 操作按钮区域 -->
      <div class="operation-section">
        <el-row :gutter="15" class="operation-row">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"
              v-hasPermi="['noticeCl:noticeCl:add']"
              class="operation-btn primary-btn"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['noticeCl:noticeCl:edit']"
              class="operation-btn success-btn"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['noticeCl:noticeCl:remove']"
              class="operation-btn danger-btn"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              v-hasPermi="['noticeCl:noticeCl:export']"
              class="operation-btn warning-btn"
            >导出</el-button>
          </el-col>
          <div class="toolbar-wrapper">
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" class="toolbar-right"></right-toolbar>
          </div>
        </el-row>
      </div>
    </el-card>

    <!-- 表格区域 -->
    <el-card class="table-card" shadow="hover">
      <div class="card-header">
        <h3 class="card-title">公告列表</h3>
        <div class="table-header-actions">
          <span class="result-count">共 {{ total }} 条记录</span>
        </div>
      </div>
      <el-table 
        v-loading="loading" 
        :data="noticeClList" 
        @selection-change="handleSelectionChange"
        class="notice-table"
        stripe
        highlight-current-row
        :header-cell-style="{background: '#fafafa', color: '#2d3a4f'}"
      >
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="主键" align="center" prop="id" /> -->
        <el-table-column label="公告内容" align="center" prop="content" min-width="300">
          <template #default="scope">
            <div class="content-cell">
              <div v-html="scope.row.content" class="notice-content"></div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="是否展示" align="center" prop="isShow" width="100">
          <template #default="scope">
            <dict-tag :options="is_or_not" :value="scope.row.isShow" :class="scope.row.isShow === 1 ? 'yes-tag' : 'no-tag'"/>
          </template>
        </el-table-column>
        <el-table-column label="显示位置" align="center" prop="position" width="120">
          <template #default="scope">
            <dict-tag :options="position" :value="scope.row.position"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="120">
          <template #default="scope">
            <span class="create-time">{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
          <template #default="scope">
            <div class="action-buttons">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['noticeCl:noticeCl:edit']" class="table-action-btn edit-btn">修改</el-button>
              <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['noticeCl:noticeCl:remove']" class="table-action-btn delete-btn">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
        class="pagination-container"
      />
    </el-card>

    <!-- 添加或修改公告管理对话框 -->
    <el-dialog v-model="open" width="700px" append-to-body class="notice-dialog">
      <div class="dialog-header">
        <h3 class="dialog-title">{{ title }}</h3>
      </div>
      <el-form ref="noticeClRef" :model="form" :rules="rules" label-width="100px" class="notice-form">
        <el-form-item label="公告内容" prop="content">
          <editor v-model="form.content" :min-height="200" class="form-editor"/>
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否展示" prop="isShow">
              <el-select v-model="form.isShow" placeholder="请选择是否展示" class="form-select">
                <el-option
                  v-for="dict in is_or_not"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示位置" prop="position">
              <el-select v-model="form.position" placeholder="请选择显示位置" class="form-select">
                <el-option
                  v-for="dict in position"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" class="submit-btn">确 定</el-button>
          <el-button @click="cancel" class="cancel-btn">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="NoticeCl">
import { listNoticeCl, getNoticeCl, delNoticeCl, addNoticeCl, updateNoticeCl } from "@/api/noticeCl/noticeCl"

const { proxy } = getCurrentInstance()
const { position, is_or_not } = proxy.useDict('position', 'is_or_not')

const noticeClList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    content: null,
    isShow: null,
    position: null,
  },
  rules: {
    content: [
      { required: true, message: "公告内容不能为空", trigger: "blur" }
    ],
    isShow: [
      { required: true, message: "是否展示不能为空", trigger: "blur" }
    ],
    position: [
      { required: true, message: "显示位置不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询公告管理列表 */
function getList() {
  loading.value = true
  listNoticeCl(queryParams.value).then(response => {
    noticeClList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    content: null,
    isShow: null,
    position: null,
    createTime: null
  }
  proxy.resetForm("noticeClRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加公告管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getNoticeCl(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改公告管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeClRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateNoticeCl(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addNoticeCl(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  
  // 构建友好的删除确认信息
  let confirmMessage = ''
  if (row.content) {
    // 单个删除，截取内容前50个字符作为提示
    const contentPreview = row.content.replace(/<[^>]+>/g, '').substring(0, 50) + (row.content.length > 50 ? '...' : '');
    confirmMessage = '是否确认删除公告 "' + contentPreview + '"'
  } else if (ids.value.length > 0) {
    // 批量删除，获取选中行的内容
    const selectedContents = []
    noticeClList.value.forEach(item => {
      if (ids.value.includes(item.id)) {
        const contentPreview = item.content.replace(/<[^>]+>/g, '').substring(0, 30) + (item.content.length > 30 ? '...' : '');
        selectedContents.push(contentPreview)
      }
    })
    
    if (selectedContents.length > 0) {
      confirmMessage = '是否确认删除公告 "' + selectedContents.join(', ') + '"'
    } else {
      confirmMessage = '是否确认删除公告管理编号为"' + _ids + '"的数据项？'
    }
  } else {
    confirmMessage = '是否确认删除公告管理编号为"' + _ids + '"的数据项？'
  }
  
  proxy.$modal.confirm(confirmMessage).then(function() {
    return delNoticeCl(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('noticeCl/noticeCl/export', {
    ...queryParams.value
  }, `noticeCl_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
.notice-container {
  background: #f0f2f5;
  padding: 20px;
  min-height: calc(100vh - 84px);
}

.search-operation-card {
  margin-bottom: 20px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  border: 1px solid #e4e7ed;
  background: #ffffff;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #e4e7ed;
  background-color: #ffffff;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #2d3a4f;
  margin: 0;
}

.search-form {
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.form-select {
  width: 100%;
}

.search-actions {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  height: 100%;
  padding-top: 10px;
}

.search-btn {
  background: #409eff;
  border: none;
  color: white;
  border-radius: 6px;
  padding: 10px 20px;
  transition: all 0.3s ease;
}

.search-btn:hover {
  background: #66b1ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(64, 158, 255, 0.3);
}

.reset-btn {
  border-radius: 6px;
  padding: 10px 20px;
  background: #f4f4f5;
  border: 1px solid #dcdfe6;
  color: #606266;
  transition: all 0.3s ease;
}

.reset-btn:hover {
  background: #e9e9eb;
  transform: translateY(-2px);
}

.operation-section {
  padding: 20px;
}

.operation-row {
  display: flex;
  align-items: center;
}

.toolbar-wrapper {
  flex: 1;
  display: flex;
  justify-content: flex-end;
}

.operation-btn {
  border-radius: 6px;
  transition: all 0.3s ease;
  padding: 10px 15px;
  font-weight: 500;
  border: 1px solid transparent;
}

.operation-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.primary-btn {
  background: #409eff;
  color: white;
  border: none;
}

.primary-btn:hover {
  background: #66b1ff;
}

.success-btn {
  background: #67c23a;
  color: white;
  border: none;
}

.success-btn:hover {
  background: #85ce61;
}

.danger-btn {
  background: #f56c6c;
  color: white;
  border: none;
}

.danger-btn:hover {
  background: #f78989;
}

.warning-btn {
  background: #e6a23c;
  color: white;
  border: none;
}

.warning-btn:hover {
  background: #ebb563;
}

.toolbar-right {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.table-card {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  border: 1px solid #e4e7ed;
  background: #ffffff;
}

.table-header-actions {
  display: flex;
  align-items: center;
}

.result-count {
  font-size: 14px;
  color: #909399;
}

.notice-table {
  width: 100%;
}

.notice-table :deep(.el-table__header th) {
  background-color: #fafafa;
  color: #2d3a4f;
  font-weight: 600;
  padding: 12px 0;
}

.notice-table :deep(.el-table__row:hover) {
  background-color: #f5f7fa;
}

.content-cell {
  padding: 10px 0;
}

.notice-content {
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
  max-height: 100px;
  overflow: hidden;
}

.create-time {
  color: #8492a6;
  font-size: 13px;
}

.yes-tag {
  color: #67c23a;
  background-color: #f0f9ec;
  border-color: #e1f3d8;
}

.no-tag {
  color: #909399;
  background-color: #f4f4f5;
  border-color: #e9e9eb;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 5px;
}

.table-action-btn {
  margin: 0 5px;
  font-size: 13px;
  padding: 6px 12px;
  border-radius: 4px;
}

.edit-btn {
  color: #409eff;
  background: #ecf5ff;
  border: 1px solid #b3d8ff;
}

.edit-btn:hover {
  background: #b3d8ff;
  color: #409eff;
  border-color: #409eff;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(64, 158, 255, 0.3);
}

.delete-btn {
  color: #f56c6c;
  background: #fef0f0;
  border: 1px solid #fbc4c4;
  transition: all 0.3s ease;
}

.delete-btn:hover {
  background: #fbc4c4;
  color: #f56c6c;
  border-color: #f56c6c;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(245, 108, 108, 0.3);
}

.pagination-container {
  padding: 20px 0;
  display: flex;
  justify-content: center;
}

.notice-dialog {
  border-radius: 12px;
}

.dialog-header {
  padding: 15px 20px;
  border-bottom: 1px solid #e4e7ed;
  background-color: #f8f9fa;
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  color: #2d3a4f;
  margin: 0;
}

.notice-dialog :deep(.el-dialog__header) {
  display: none;
}

.notice-form {
  padding: 20px;
}

.form-editor {
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  overflow: hidden;
}

.submit-btn {
  border-radius: 6px;
  padding: 12px 24px;
  background: #409eff;
  border: none;
  color: white;
  font-weight: 500;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  background: #66b1ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
}

.cancel-btn {
  border-radius: 6px;
  padding: 12px 24px;
  background: #f4f4f5;
  border: 1px solid #dcdfe6;
  color: #606266;
  font-weight: 500;
  transition: all 0.3s ease;
}

.cancel-btn:hover {
  background: #e9e9eb;
  transform: translateY(-2px);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .notice-container {
    padding: 15px;
  }
  
  .search-operation-card,
  .table-card {
    padding: 15px;
  }
  
  .notice-table :deep(.el-table__column) {
    font-size: 12px;
  }
  
  .operation-row {
    flex-wrap: wrap;
  }
  
  .operation-row .el-col {
    margin-bottom: 10px;
  }
}

@media (max-width: 992px) {
  .notice-container {
    padding: 15px;
  }
  
  .search-form :deep(.el-col) {
    margin-bottom: 15px;
  }
  
  .search-actions {
    justify-content: flex-end;
  }
  
  .operation-row {
    padding: 15px 0;
  }
}

@media (max-width: 768px) {
  .notice-container {
    padding: 10px;
  }
  
  .notice-dialog {
    width: 95% !important;
    margin-top: 20px !important;
  }
  
  .card-header {
    padding: 12px 15px;
  }
  
  .card-title {
    font-size: 16px;
  }
  
  .search-form,
  .operation-section {
    padding: 15px;
  }
  
  .notice-content {
    font-size: 13px;
  }
}
</style>